package com.skymxc.wandroidtodo.page.update;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.skymxc.wandroidtodo.R;
import com.skymxc.wandroidtodo.databinding.ActivityUpdateItemBinding;
import com.skymxc.wandroidtodo.entity.TODOItem;
import com.skymxc.wandroidtodo.page.base.BaseActivity;

/**
 * <p>
 *
 * </p>
 *
 * @author 孟祥超
 * <p>
 * date: 2021/12/6  7:12 上午
 */
public class UpdateItemActivity extends BaseActivity<UpdateItemPresenter>
        implements UpdateItemContact.UpdateItemView, View.OnClickListener {

    private static final String ART_ITEM = "arg.item";

    public static Intent newIntent(Context context, TODOItem item) {
        Intent intent = new Intent(context, UpdateItemActivity.class);
        intent.putExtra(ART_ITEM, item);
        return intent;
    }

    @Override
    protected UpdateItemPresenter initPresenter() {
        return new UpdateItemPresenter(getApplication());
    }


    private ActivityUpdateItemBinding mBinding;

    private boolean updated = false;

    @Override
    protected void initView() {
        super.initView();
        mBinding = ActivityUpdateItemBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        mBinding.updateBtn.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        TODOItem item = getIntent().getParcelableExtra(ART_ITEM);
        mPresenter.setUpdateItem(item);
        mBinding.updateEtContent.setText(item.getContent());
        mBinding.updateEtTitle.setText(item.getTitle());
    }

    @Override
    public void onBackPressed() {
        if (updated) {
            setResult(RESULT_OK);
        }
        super.onBackPressed();
    }

    @Override
    public void updateSuccess() {
        showConfirm("修改成功");
        updated = true;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.update_btn:
                String title = mBinding.updateEtTitle.getText().toString();
                if (TextUtils.isEmpty(title)) {
                    showConfirm("请输入标题");
                    return;
                }
                String content = mBinding.updateEtContent.getText().toString();

                mPresenter.updateItem(title, content);
                break;
        }
    }
}
